package sf.database.template.enjoy;

import com.jfinal.template.source.StringSource;
import sf.database.OrmConfig;
import sf.database.jdbc.sql.SQLContext;
import sf.database.jdbc.sql.SQLParameter;
import sf.database.template.TemplateHandler;
import sf.database.template.TemplateRender;
import sf.database.template.enjoy.ext.SqlPara;
import sf.spring.core.io.Resource;
import sf.tools.IOUtils;
import sf.tools.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * enjoy模板实现类
 */
public class EnjoyHandler implements TemplateHandler {

    private static boolean initialized = false;

    private static EnjoyHandler instance;

    public static EnjoyHandler getInstance() {
        if (instance == null) {
            instance = new EnjoyHandler();
        }
        return instance;
    }


    private EnjoyHandler() {

    }


    @Override
    public void loadAllSQL() {
        Set<Resource> set = TemplateRender.getAllSqlTemplate();
        for (Resource r : set) {
            try {
                String content = IOUtils.asString(r.getInputStream());
                //必须有内容
                if (StringUtils.isNotBlank(content)) {
                    StringSource ss = new StringSource(content, true);
                    EnjoyHelp.getEnjoySqlKit().addSqlTemplate(ss);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        EnjoyHelp.getEnjoySqlKit().parseSqlTemplate();
        initialized = true;
    }

    @Override
    public String getSQL(String sqlId) {
        if (!initialized) {
            loadAllSQL();
            //是否开启debug模式
            if (!OrmConfig.getInstance().isSqlTemplateDebug()) {// 非debug模式则初始化一次
                initialized = true;
            }
        }
        return null;
    }

    @Override
    public SQLContext getParsedSql(String sqlId, Map<String, Object> paramters) {
        getSQL(null);
        SqlPara sp = EnjoyHelp.getEnjoySqlKit().getSqlPara(sqlId, paramters);
        return getSQLContext(sp);
    }


    public SQLContext getSQLContext(SqlPara sp) {
        SQLContext context = new SQLContext();
        String sql = sp.getSql();
//        sql = SqlHelp.replaceLineSql(sql);
        context.setSql(sql);
        if (sp.getPara() != null) {
            List<SQLParameter> list = new ArrayList<>();
            for (Object o : sp.getPara()) {
                SQLParameter p = new SQLParameter();
                p.setValue(o);
                list.add(p);
            }
            context.setParas(list);
        }
        return context;
    }

    @Override
    public SQLContext getParsedPageSql(String sqlId, Map<String, Object> paramters) {
        getSQL(null);
        SqlPara sp = EnjoyHelp.getEnjoySqlKit().getSqlParaPage(sqlId, paramters);
        return getPageSQLContext(sp);
    }

    @Override
    public SQLContext getParsedPageSqlSource(String sqlId, Map<String, Object> paramters) {
        getSQL(null);
        SqlPara sp = EnjoyHelp.getEnjoySqlKit().getSqlParaPageSource(sqlId, paramters);
        return getPageSQLContext(sp);
    }

    public SQLContext getPageSQLContext(SqlPara sp) {
        SQLContext context = new SQLContext();
        String sql = sp.getListSql();//
//        sql = SqlHelp.replaceLineSql(sql);
        context.setListSql(sql);
        String countSql = sp.getCountSql();
//        countSql = SqlHelp.replaceLineSql(countSql);
        context.setCountSql(countSql);
        if (sp.getPara() != null) {
            List<SQLParameter> list = new ArrayList<>();
            for (Object o : sp.getPara()) {
                SQLParameter p = new SQLParameter();
                p.setValue(o);
                list.add(p);
            }
            context.setParas(list);
        }
        return context;
    }

    @Override
    public SQLContext getParsedSqlSource(String sqlTemplate, Map<String, Object> paramters) {
        SqlPara sp = EnjoyHelp.getEnjoySqlKit().getSqlParaBySource(sqlTemplate, paramters);
        return getSQLContext(sp);
    }

    @Override
    public SQLContext getParsedPageCountSql(String sqlId, Map<String, Object> paramters) {
        return null;
    }

    @Override
    public SQLContext getParsedPageCountSqlSource(String sqlTemplate, Map<String, Object> paramters) {
        return null;
    }

    @Override
    public SQLContext getParsedPageListSql(String sqlId, Map<String, Object> paramters) {
        return null;
    }

    @Override
    public SQLContext getParsedPageListSqlSource(String sqlTemplate, Map<String, Object> paramters) {
        return null;
    }
}
